Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
pretty-format
Advanced tools
The pretty-format npm package is a JavaScript library that allows you to serialize any JavaScript value into a string with a human-readable format. It is particularly useful for snapshot testing, where you want to compare the expected and actual output of your test cases.
Pretty-printing of basic JavaScript types
This feature allows you to convert basic JavaScript types like objects, arrays, strings, numbers, etc., into a nicely formatted string.
const prettyFormat = require('pretty-format');
const value = { foo: 'bar', baz: 42 };
console.log(prettyFormat(value));
Customizing output with plugins
pretty-format supports plugins that can be used to customize the output for specific types of values, such as React elements.
const prettyFormat = require('pretty-format');
const ReactElementPlugin = require('pretty-format/plugins/ReactElement');
const reactElement = <div>Hello World</div>;
console.log(prettyFormat(reactElement, { plugins: [ReactElementPlugin] }));
Minimizing diff output
By using pretty-format in combination with a diffing library like jest-diff, you can minimize the output of diffs to make them easier to read and understand.
const prettyFormat = require('pretty-format');
const diff = require('jest-diff');
const oldValue = { a: 'old', b: 'values' };
const newValue = { a: 'new', b: 'values' };
const difference = diff(prettyFormat(oldValue), prettyFormat(newValue));
console.log(difference);
Chalk is a popular npm package for styling terminal strings. While it doesn't serialize objects, it can be used in conjunction with pretty-format to colorize the output, enhancing readability.
Util is a core Node.js module that provides a method called 'inspect' for printing objects in a readable format. It is similar to pretty-format but is built into Node.js and does not support plugins.
js-beautify is an npm package that can format HTML, CSS, and JavaScript code. It is more focused on formatting code rather than serializing arbitrary JavaScript values like pretty-format.
Stringify any JavaScript value.
JSON.stringify
and significantly faster than Node's util.format
)ReactTestComponent
)$ yarn add pretty-format
const prettyFormat = require('pretty-format');
var obj = { property: {} };
obj.circularReference = obj;
obj[Symbol('foo')] = 'foo';
obj.map = new Map();
obj.map.set('prop', 'value');
obj.array = [1, NaN, Infinity];
console.log(prettyFormat(obj));
Result:
Object {
"property": Object {},
"circularReference": [Circular],
"map": Map {
"prop" => "value"
},
"array": Array [
1,
NaN,
Infinity
],
Symbol(foo): "foo"
}
Object
, Array
, ArrayBuffer
, DataView
, Float32Array
, Float64Array
, Int8Array
, Int16Array
, Int32Array
, Uint8Array
, Uint8ClampedArray
, Uint16Array
, Uint32Array
, arguments
, Boolean
, Date
, Error
, Function
, Infinity
, Map
, NaN
, null
, Number
, RegExp
, Set
, String
, Symbol
, undefined
, WeakMap
, WeakSet
console.log(prettyFormat(object));
console.log(prettyFormat(object, options));
Options:
callToJSON
boolean
, default: true
toJSON()
on passed object.indent
number
, default: 2
maxDepth
number
, default: Infinity
min
boolean
, default: false
plugins
array
, default: []
printFunctionName
boolean
, default: true
[Function]
.escapeRegex
boolean
, default: false
highlight
boolean
, default: false
ReactTestComponent
and ReactElement
plugins.theme
object
, default: {tag: 'cyan', content: 'reset'...}
reset
for no color.tag
, content
, prop
and value
.Pretty format also supports adding plugins:
const fooPlugin = {
test(val) {
return val && val.hasOwnProperty('foo');
},
print(val, print, indent) {
return 'Foo: ' + print(val.foo);
}
};
const obj = {foo: {bar: {}}};
prettyFormat(obj, {
plugins: [fooPlugin]
});
// Foo: Object {
// "bar": Object {}
// }
ReactTestComponent
and ReactElement
pluginsconst prettyFormat = require('pretty-format');
const reactTestPlugin = require('pretty-format/build/plugins/ReactTestComponent');
const reactElementPlugin = require('pretty-format/build/plugins/ReactElement');
const React = require('react');
const renderer = require('react-test-renderer');
const element = React.createElement('h1', null, 'Hello World');
prettyFormat(renderer.create(element).toJSON(), {
plugins: [reactTestPlugin, reactElementPlugin]
});
// <h1>
// Hello World
// </h1>
jest 18.1.0
context
variable.-h
as alias for --help
.FAQs
Stringify any JavaScript value.
The npm package pretty-format receives a total of 32,133,183 weekly downloads. As such, pretty-format popularity was classified as popular.
We found that pretty-format demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.